package com.bodystm.hardwarecavas;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.CopyOnWriteArrayList;

import androidx.annotation.RequiresApi;

/**
 * 心电波形的绘制
 * 每秒绘制水平方向25毫米物理距离
 * 调用.so文件的算法返回的数据单位为毫伏 1毫伏 = 25mm（毫米） 上下波动
 */
public class ReviewEcgView extends SurfaceView implements SurfaceHolder.Callback {

    class EcgPainDataBean {
        public EcgPainDataBean(float ecgData, boolean beat, float paceVal) {
            this.ecgData = ecgData;
            this.beat = beat;
            this.paceVal = paceVal;
        }
        public float ecgData;
        public boolean beat;
        public float paceVal;
    }
    private boolean hardwareRun = true; // GPU硬件加速绘图
    private Context mContext;
    public boolean isRunning = false; //是否开始
    public boolean stop = false;
    private boolean canDraw = false;

    public float ecgMax = 4096;//屏幕的高度
    private int bgColor = R.color.color_171717; //背景颜色
    private float wave_speed = 25;//波速: 25mm/s
    private int sleepTime = 30; //每次锁屏的时间间距，单位:ms 每25毫秒绘制n个点
    private int lockWidth;//每次绘制的宽度 单位: Px
    private int ecgPerCount = 3;//每次画心电数据的个数 Y轴的点数
    private float noneUp = 1;
    private int paceFlagDrawType = 1;   //0：替换心电数据，1：位置绘制竖线
    //数据存储
    public CopyOnWriteArrayList<Float> ecg0Datas = new CopyOnWriteArrayList<Float>();
    public CopyOnWriteArrayList<Boolean> heartbeatList = new CopyOnWriteArrayList<Boolean>();
    public CopyOnWriteArrayList<Float> paceFlagList = new CopyOnWriteArrayList<Float>();
    public CopyOnWriteArrayList<EcgPainDataBean> ecgPainDataList = new CopyOnWriteArrayList<EcgPainDataBean>();


    private Paint mPaint;//画波形图的画笔
    private Paint mVoltagePaint;//画波形图的画笔
    private Paint mPaintBg;//画背景的画笔
    private Bitmap mBitmap;//临时画布
    private Canvas mineCanvas;//临时画布操作canvas
    private int mWidth;//控件宽度
    private int mHeight;//控件高度
    private int startY0;
    private int VoltageStartY0;
    private Rect rect;

    private float startX;//每次画线的X坐标起点
    private float voltageStartX;//每次画线的X坐标起点
    private int ecgXOffset;//每次X坐标偏移的像素
    private int blankLineWidth = 18;//右侧空白点的宽度
    private int linColor = R.color.color_10FF18;
    public static double px1mm = 0;
    private boolean isGrid = false;
    private boolean waveFreeze = false;
    private boolean voltageWave = false;

    public void setGrid(boolean isGrid) {
        this.isGrid = isGrid;
    }

    public void setWaveFreeze(boolean waveFreeze) {
        this.waveFreeze = waveFreeze;
    }

    public void setVoltageWave(boolean voltageWave) {
        this.voltageWave = voltageWave;
    }

    public void setBackGroundColor(int color) {
        this.bgColor = color;
    }

    public void setPaintColor(int color) {
        this.linColor = color;
        if (mPaint != null)
            mPaint.setColor(mContext.getResources().getColor(linColor));
    }

    public void setNoneUp(float noneUp) {
        this.noneUp = noneUp;
    }

    public ReviewEcgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        // 初始化波形速度
        int waveSpeedMode = 1;//(int) SharedPreferencesUtils.getParam(Constant.EcgWaveSpeed, Constant.EcgWaveSpeedDef);
        if (waveSpeedMode == 0) {
            wave_speed = 12.5f;
            drawSampling = 100;//200;
        }
        else if (waveSpeedMode == 1) {
            wave_speed = 25f;
            drawSampling = 100;//200;
        }
        else if (waveSpeedMode == 2) {
            wave_speed = 50f;
            drawSampling = 100;//200;
        }

        getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
        getHolder().addCallback(this);
        //背景透明
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        rect = new Rect();
        converXOffset();

    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(mContext.getResources().getColor(linColor));  //画笔颜色
        mPaint.setAntiAlias(true);  //去锯齿
        mPaint.setStrokeWidth(2); //画笔的粗度
        mVoltagePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mVoltagePaint.setColor(mContext.getResources().getColor(R.color.color_CC20FF10));  //画笔颜色
        mVoltagePaint.setAntiAlias(true);  //去锯齿
        mVoltagePaint.setStrokeWidth(2); //画笔的粗度
        mPaintBg = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintBg.setColor(mContext.getResources().getColor(bgColor));  //画笔颜色
        ecgXOffset = lockWidth / 3;
        startY0 = mHeight / 2;//波1初始Y坐标是控件高度的1/2
    }

    /**
     * 根据波速计算每次X坐标增加的像素
     * 计算出每次锁屏应该画的px值
     */
    private void converXOffset() {
        WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        manager.getDefaultDisplay().getRealSize(point);
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        int densityDpi = dm.densityDpi;//得到屏幕的密度值，但是该密度值只能作为参考，因为他是固定的几个密度值。
        double x = Math.pow(point.x / dm.xdpi, 2);//dm.xdpi是屏幕x方向的真实密度值，比上面的densityDpi真实。
        double y = Math.pow(point.y / dm.ydpi, 2);//dm.xdpi是屏幕y方向的真实密度值，比上面的densityDpi真实。
        double screenInches = Math.sqrt(x + y);
        // todo系统参数不正确，手动修正尺寸
        screenInches = 4.3f;
        //获取屏幕对角线的长度 单位：px
        double diagonalPixels = Math.sqrt(Math.pow(point.x, 2) + Math.pow(point.y, 2));
        Log.e("mmNumber", "point.x: " + point.x + " point.y: " + point.y + " dm.xdpi: " + dm.xdpi + "  dm.ydpi : " + dm.ydpi + "  x : " + x + "  y : " + y);
        double inchByPixels = diagonalPixels/screenInches;
        //英寸数
        double widthScreenInch = point.x / inchByPixels;
        //屏幕宽度的毫米数
        double mmNumber = widthScreenInch * 25.4;
        //每毫米有多少px
        px1mm = point.x / mmNumber;
    }

    /**
     * 是否执行了波速调整
     */
    private boolean speedChanged = false;
    /**
     * 波形速度变化
     * @param config
     */
//    @Subscribe(threadMode = ThreadMode.BACKGROUND)
//    public void changeWave(EcgWaveConfig config) {
//        float speed = config.waveSpeed;
//        if (wave_speed == speed)
//            return;
//        if(speed == 12.5f) {
//            wave_speed = speed;
//            drawSampling = 200;
//        } else if (speed == 50) {
//            wave_speed = speed;
//            drawSampling = 250;
//        } else {
//            wave_speed = 25;
//            drawSampling = 200;
//        }
//        speedChanged = true;
//        //每秒画多少px
//        double px1s = wave_speed * px1mm;
//        lockWidth = (int) (px1s * (sleepTime / 1000f));
//        ecgXOffset = lockWidth / 3;
//    }

    private boolean onOff = false;
    private int refreshCount = 0;

    public void onOff() {
        if (!onOff && refreshCount < 3) {
            refreshCount++;
            // rect 要获取画布中的区域
            rect.set(0, 0, mWidth, mHeight);
            Canvas mCanvas = getHolder().lockCanvas(rect);
            if (mCanvas == null) return;
            if (isGrid) {
                mCanvas = drawGrid(mCanvas);
            } else {
                int color = mContext.getResources().getColor(bgColor);
                mPaintBg.setColor(color);
                mCanvas.drawRect(rect, mPaintBg);
            }
            getHolder().unlockCanvasAndPost(mCanvas);
        }

        if (refreshCount >= 3) {
            onOff = true;
        }
    }

    /**
     * 得到屏幕的物理尺寸，由于该尺寸是在出厂时，厂商写死的，所以仅供参考
     * 计算方法：获取到屏幕的分辨率:point.x和point.y，再取出屏幕的DPI（每英寸的像素数量），
     * 计算长和宽有多少英寸，即：point.x / dm.xdpi，point.y / dm.ydpi，屏幕的长和宽算出来了，
     * 再用勾股定理，计算出斜角边的长度，即屏幕尺寸。
     *
     * @return 屏幕的英寸
     */
//    public double getPhysicsScreenSize() {
//        WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
//        Point point = new Point();
//        manager.getDefaultDisplay().getRealSize(point);
//        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
//        int densityDpi = dm.densityDpi;//得到屏幕的密度值，但是该密度值只能作为参考，因为他是固定的几个密度值。
//        double x = Math.pow(point.x / dm.xdpi, 2);//dm.xdpi是屏幕x方向的真实密度值，比上面的densityDpi真实。
//        double y = Math.pow(point.y / dm.ydpi, 2);//dm.xdpi是屏幕y方向的真实密度值，比上面的densityDpi真实。
//        double screenInches = Math.sqrt(x + y);
//        Log.e("mmNumber", "point.x: " + point.x + " point.y: " + point.y + " dm.xdpi: " + dm.xdpi + "  dm.ydpi : " + dm.ydpi + "  x : " + x + "  y : " + y);
//        return screenInches;
//    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        startX = 0;
        canDraw = false;
        // rect 要获取画布中的区域
        Canvas mCanvas;
        if (hardwareRun) {
            mCanvas = holder.lockHardwareCanvas();
        } else {
            mCanvas = holder.lockCanvas();
        }
        if (mCanvas == null) return;
        if (isGrid) {
            mCanvas = drawGrid(mCanvas);
        } else {
            int color = mContext.getResources().getColor(bgColor);
            mCanvas.drawColor(color);
        }
        holder.unlockCanvasAndPost(mCanvas);
        startThread();
    }

    private Canvas drawGrid(Canvas mCanvas) {
        int color = mContext.getResources().getColor(bgColor);
        mCanvas.drawColor(color);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(0.5f);
        double xRemain = 0f;
        for (int i = 0, j = 1; i < mHeight; j++) {
            if (j % 5 == 0)
                paint.setColor(mContext.getResources().getColor(R.color.color_CCFF0000));//color_CCFF0000
            else
                paint.setColor(mContext.getResources().getColor(R.color.color_80FA0000));//color_80FA0000
            mCanvas.drawLine(0, i, mWidth, i, paint);
            i += (int)(px1mm+xRemain);
            xRemain = (px1mm+xRemain) - (int)(px1mm+xRemain);
        }
        double yRemain = 0f;
        for (int i = 0, j = 1; i < mWidth;  j++) {
            if (j % 5 == 0)
                paint.setColor(mContext.getResources().getColor(R.color.color_CCFF0000));//color_CCFF0000
            else
                paint.setColor(mContext.getResources().getColor(R.color.color_80FA0000));//color_80FA0000
            mCanvas.drawLine(i, 0, i, mHeight, paint);
            i += (int)(px1mm+yRemain);
            yRemain = (px1mm+yRemain) - (int)(px1mm+yRemain);
        }

        return mCanvas;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;   //View的宽 单位：PX
        mHeight = h; //View的高 单位：PX
        if (hardwareRun) {
            if (mBitmap == null) {
                mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
                mineCanvas = new Canvas(mBitmap);
            }
        }
        isRunning = true;
        ecgMax = h;
        init();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        stopThread();
    }

    public void startThread() {
//        EventBus.getDefault().register(this);
        isRunning = true;
    }

    public void stopThread() {
        canDraw = false;
        isRunning = false;
        if (ecgPainDataList.size() > 0) {
            ecgPainDataList.clear();
        }
//        EventBus.getDefault().unregister(this);
    }

    private float simData[] = {
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f,
    };
    private float simData2[] = {
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
            0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f,
    };
    private int readIndex = 0;
    //    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    @RequiresApi(api = Build.VERSION_CODES.O)
    public void onAlertChangeEvent(DrawReviewWaveBean bean) {
        // todo 模拟增加一些数据
        for (int i = 0; i < bean.count; i++) {
            addEcgData0(simData2[readIndex++]);
            readIndex %= 100;
        }

        if (isRunning && !waveFreeze) {
//            if (wave_speed == 50) {
//                ecgPerCount = bean.count3;
//            } else {
//                ecgPerCount = bean.count2;
//            }
            ecgPerCount = bean.count;
            if (ecgPerCount > 0)
                startDrawWave();
        } else {
            if (ecgPainDataList.size() > 0) {
                ecgPainDataList.clear();
            }
        }
    }

    /**
     * 上次计算的绘制宽带剩余的小数部分
     */
    private double lastRemainPix = 0f;
    /**
     * 绘制的时间转换要绘制的点数
     * @param interval
     * @return
     */
    private int interval2pix(int interval) {
        double curDrawPixWidth = wave_speed * interval / 1000.0f * px1mm + lastRemainPix;
        int drawPix = (int)curDrawPixWidth;
        lastRemainPix = curDrawPixWidth - drawPix;
//        Log.e(TAG, "interval is " + interval + ", curDrawPixWidth is "+curDrawPixWidth+", draw pix is " + drawPix + ", remain pix is " + lastRemainPix);
        return drawPix;
    }
    /**
     * 绘制的时间转换要绘制的点数,不考虑小数部分
     * @param interval
     * @return
     */
    private int interval2pixEx(int interval) {
        double curDrawPixWidth = wave_speed * interval / 1000.0f * px1mm + lastRemainPix;
        int drawPix = (int)curDrawPixWidth;
        return drawPix;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startDrawWave() {
        int curDrawWidth = interval2pixEx(ecgPerCount * 1000/drawSampling);
//        Log.e("liuyue-startDrawWave", "ecgXOffset " + ecgXOffset + ", ecgPerCount " + ecgPerCount);
        rect.set((int) startX, 0, (int) (startX + curDrawWidth + blankLineWidth), mHeight);
        synchronized (this) {
            if (!onOff) {
                Surface surface = getHolder().getSurface();
                Canvas mCanvas;
                if (hardwareRun) {
                    mCanvas = surface.lockHardwareCanvas();
                } else {
                    mCanvas = surface.lockCanvas(rect);
                    mineCanvas = mCanvas;
                }
                if (mCanvas == null)  {
                    return;
                }
                if (isGrid) {
                    mineCanvas = drawGrid(mineCanvas);
                } else {
                    int color = mContext.getResources().getColor(bgColor);
                    mPaintBg.setColor(color);
                    mineCanvas.drawRect(rect, mPaintBg);
//                    mCanvas.drawB
                }
                //数据域过长删除删除
//                if (startX == 0 && ecgPainDataList.size() > drawSampling*2) {
//                    int removeCount = ecgPainDataList.size() - drawSampling;
//                    while (removeCount > 0) {
//                        if (ecgPainDataList.size() != 0) {
//                            ecgPainDataList.remove(0);
//                        }
//                        removeCount--;
//                    }
//                }
                //绘制波形
                try {
                    drawWave0(mineCanvas);
                    if (hardwareRun) {
                        mCanvas.drawBitmap(mBitmap, 0, 0, mPaintBg);
                    }
                } finally {
                    surface.unlockCanvasAndPost(mCanvas);
                }
            }
        }
    }

    private void drawWave0(Canvas mCanvas) {
        try {
            //x的开始位置
            float mStartX = startX;
            //判断数据长度是否大于当前要绘制的数据 并 数据长度大于0
            int curRealDrawPix = 0;
            if (ecgPainDataList.size() > ecgPerCount && ecgPainDataList.size() > 0 && canDraw) {
                for (int i = 0; i < ecgPerCount; i++) {
                    ecgXOffset = interval2pix(1000 / drawSampling);
                    curRealDrawPix += ecgXOffset;
                    Float data;
                    EcgPainDataBean bean;
                    if (ecgPainDataList.size() > 0) {
                        bean = ecgPainDataList.get(0);
                        data = bean == null ? null : bean.ecgData;
                        boolean paceDrawLine = false;
                        if (bean != null && bean.paceVal != 0) {
                            if (paceFlagDrawType == 0)
                                data = bean.paceVal;
                            else {
                                paceDrawLine = true;
                            }
                        }
                        ecgPainDataList.remove(0);
                        if (bean != null && bean.beat) {
                            EventBus.getDefault().post(Constant.HeartSound);
                        }
                        if (null == data) {
                            data = 0.0f;
                        }
                        float newX = mStartX + ecgXOffset;
                        int newY = ecgConver((double)data);
                        if (mStartX == 0) {
                            startY0 = newY;
                        }
                        if (paceDrawLine) {
                            int paceY = ecgConver((double)bean.paceVal);
                            mCanvas.drawLine(mStartX, startY0, mStartX, paceY, mPaint);
                        }

                        // 在画布上绘制线 mStartX:线开始绘制的x坐标位置 startY0:线开始绘制的y坐标位置 newX:线结束的x坐标位置 newY:线结束的Y坐标位置
                        mCanvas.drawLine(mStartX, startY0, newX, newY, mPaint);

                        mStartX = newX;
                        startY0 = newY;
                    }
                }
            } else {
                /**
                 * 如果没有数据
                 * 因为有数据一次画ecgPerCount个数，那么无数据时候就应该画ecgPercount倍数长度的中线
                 */
                curRealDrawPix = interval2pix(1000 / drawSampling * ecgPerCount);
                float newX = mStartX + (ecgXOffset * ecgPerCount);
                int newY = mHeight / 2;
                // 波形回顾功能中无数据不绘制
//                if (canDraw)
                    mCanvas.drawLine(mStartX, startY0, newX, newY, mPaint);
                startY0 = newY;
                if ((ecgPainDataList.size() < ecgPerCount || ecgPainDataList.size() <= 0) && canDraw) {
                    canDraw = false;
                }
            }
            startX = startX + curRealDrawPix;
            if (startX > mWidth) {
                startX = 0;
            }
        } catch (NoSuchElementException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将心电数据转换成用于显示的Y坐标
     *
     * @param data
     * @return
     */
    private int ecgConver(Double data) {
        int px10mm = (int) (px1mm * 10);
        BigDecimal bg = new BigDecimal(data);
        int count = (int) (((bg.doubleValue() * noneUp) * px10mm) + (ecgMax / 2));
        //因为view的坐标起点是左上角 所以数据要做处理
        if (count < 0) {
            count = (int) ((count * -1) + ecgMax);
        } else if (count <= ecgMax) {
            count = (int) (ecgMax - count);
        } else {
            count = (int) ((count - ecgMax) * -1);
        }
        return count;
    }
    // 心电心跳音标记
    private boolean ecgPear = false;
    // 起搏信号标记
    private boolean ecgPaceFlag = false;
    private float ecgPaceVal = 0f;
    // 波形绘制重采样
    private Resampling resampling = new Resampling();
    // 输入采样率
    private final int inputSampling = 100;//Constant.EcgSampling;
    // 绘制采样率
    private int drawSampling = 100;//200;

    /**
     * 添加数据
     *
     * @param data
     */
    public void addEcgData0(Float data, boolean heartbeat, Float paceVal) {
        if (!waveFreeze) {
            if (speedChanged) {
                speedChanged = false;
                canDraw = false;
                ecgPainDataList.clear();
            }
            int nLoop = resampling.Resampling(drawSampling, inputSampling, 0);
            if (!ecgPear) ecgPear = heartbeat;
            if (ecgPaceVal == 0) { ecgPaceVal = paceVal;}
            for (int j = nLoop; j >= 0; j--) {
//                // todo 模拟效果
//                if (ecgPear) ecgPaceVal = 5f;
                ecgPainDataList.add(new EcgPainDataBean(data, ecgPear, ecgPaceVal));

                onOff = false;
                refreshCount = 0;
                if (ecgPainDataList.size() > drawSampling)
                    canDraw = true;
                ecgPear = false;
                ecgPaceVal = 0f;
            }

        }
    }

    public void addEcgData0(List<Float> data, List<Boolean> heartbeats, List<Float> paceVals) {
        if (!waveFreeze) {
            if (speedChanged) {
                speedChanged = false;
                canDraw = false;
                ecgPainDataList.clear();
            }
            if (data.size() != heartbeats.size() || heartbeats.size() != paceVals.size())
                return;
            if (drawSampling != inputSampling) {
                for (int i = 0; i < data.size(); i++) {
                    addEcgData0(data.get(i), heartbeats.get(i), paceVals.get(i));
                }
            } else {
                for (int i = 0; i < data.size(); i++) {
                    // todo 模拟效果
                    if (heartbeats.get(i))
                        ecgPainDataList.add(new EcgPainDataBean(data.get(i), heartbeats.get(i), 5f));
                    else
                        ecgPainDataList.add(new EcgPainDataBean(data.get(i), heartbeats.get(i), paceVals.get(i)));
                }
                onOff = false;
                refreshCount = 0;
                if (ecgPainDataList.size() > drawSampling)
                    canDraw = true;
            }
        }
    }

    /**
     * 添加数据
     *
     * @param data
     */
    public void addEcgData0(Float data) {
        if (!waveFreeze) {
            ecgPainDataList.add(new EcgPainDataBean(data, false, 0f));
            onOff = false;
            refreshCount = 0;
            if (ecgPainDataList.size() > drawSampling)
                canDraw = true;
        }
    }

    public void addEcgData0(List<Float> data) {
        if (!waveFreeze) {
            for (Float val : data) {
                ecgPainDataList.add(new EcgPainDataBean(val, false, 0f));
            }
            onOff = false;
            refreshCount = 0;
            if (ecgPainDataList.size() > drawSampling)
                canDraw = true;
        }
    }
}